﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using Hxj.Data;
using System.Data;
using Utility.Model;

namespace MsgCenter.Web.Pages.Dic
{
    public partial class Option : BaseUI.BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strGuID = ParamsOfRequest.Request("Guid");
        protected string strOptionType = ParamsOfRequest.Request("OptionType");
        public string OrderByListItems_UniqueID;
        protected void Page_Load(object sender, EventArgs e)
        {
            OrderByListItems_UniqueID = this.lstBox.UniqueID.Replace("$", "_");
            if (strOptionType == "One")
            {
                linkSave.OnClientClick = "return CheckSubmit();";
            }
            else if (strOptionType == "Dic")
            {
                linkSave.OnClientClick = "return CheckSubmitValue();";
            }
            else if (strOptionType == "DicValue")
            {
                linkSave.OnClientClick = "return CheckSubmitValue();";
            }
            if (!this.IsPostBack)
            {
                this.pnlDic.Visible = this.pnlValue.Visible = this.pnlSort.Visible = this.linkSave.Visible = false;
                if (strType == "Sort")
                {
                    GetSortData();
                }
                else
                {
                    if (strOptionType == "One")
                    {
                        if (strType == "Add")
                        {
                            this.linkSave.Visible = this.pnlDic.Visible = true;
                            this.pnlValue.Visible = false;
                        }
                    }
                    else if (strOptionType == "Dic")
                    {
                        GetDicData();
                    }
                    else if (strOptionType == "DicValue")
                    {
                        if (strType == "Edit")
                        {
                            this.linkSave.Visible = this.pnlValue.Visible = true;
                            Sys_Dic model = BLL.BLLServiceHelper.GetDicService().GetModelDic(strGuID);
                            if (model != null)
                            {
                                txtDicValueName.Value = model.DicName;
                                txtDicValueDes.Text = model.DicDes;
                                chkValueBox.Checked = !model.IsDelete.Value;
                            }
                        }
                    }
                }
            }
        }

        private void GetDicData()
        {
            if (strType == "Add")
            {
                this.pnlDic.Visible = false;
                this.linkSave.Visible = this.pnlValue.Visible = true;
            }
            else if (strType == "Edit")
            {
                this.linkSave.Visible = this.pnlDic.Visible = true;
                this.pnlValue.Visible = false;
                Sys_Dic model = BLL.BLLServiceHelper.GetDicService().GetModelDic(strGuID);
                if (model != null)
                {
                    txtName.Value = model.DicName;
                    txtDes.Text = model.DicDes;
                    chkBox.Checked = !model.IsDelete.Value;
                }
            }
        }

        private void GetSortData()
        {
            this.linkSave.Visible = this.pnlSort.Visible = true;
            if (strOptionType == "One")
            {
                WhereClip wcp = new WhereClip();
                wcp = wcp.And(Sys_Dic._.ParentDicID == Guid.Empty);
                using (DataTable dtDic = BLL.BLLServiceHelper.GetDicService().GetDicTable(wcp))
                {
                    DataRow[] drs = dtDic.Select("", "Sort");
                    foreach (DataRow dr in drs)
                    {
                        this.lstBox.Items.Add(new ListItem(dr["DicName"].ToString(), dr["DicID"].ToString()));
                    }
                    if (dtDic.Rows.Count == 0)
                    {
                        this.linkSave.Visible = false;
                    }
                }
            }
            else if (strOptionType == "Dic")
            {
                WhereClip wcp = new WhereClip();
                wcp = wcp.And(Sys_Dic._.ParentDicID == strGuID);
                using (DataTable dtDic = BLL.BLLServiceHelper.GetDicService().GetDicTable(wcp))
                {
                    DataRow[] drs = dtDic.Select("", "Sort");
                    foreach (DataRow dr in drs)
                    {
                        this.lstBox.Items.Add(new ListItem(dr["DicName"].ToString(), dr["DicID"].ToString()));
                    }
                    if (dtDic.Rows.Count == 0)
                    {
                        this.linkSave.Visible = false;
                    }
                }
            }
        }

        protected void linkSave_Click(object sender, EventArgs e)
        {
            if (strType == "Sort")
            {
                try
                {
                    BLL.BLLServiceHelper.GetDicService().Update_Sort(hidValue.Value);
                    base.ShowAlertScript("Close_Reload();");
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
            else
            {
                if (strOptionType == "One")
                {
                    CreateDic();
                }
                else if (strOptionType == "Dic")
                {
                    if (strType == "Add")
                    {
                        CreateDicValue();
                    }
                    else if (strType == "Edit")
                    {
                        CreateDic();
                    }
                }
                else if (strOptionType == "DicValue")
                {
                    EditDicValue();
                }
            }
        }

        private void EditDicValue()
        {
            if (strType == "Edit")
            {
                try
                {
                    Sys_Dic model = BLL.BLLServiceHelper.GetDicService().GetModelDic(strGuID);
                    if (model != null)
                    {
                        model.DicName = txtDicValueName.Value;
                        model.DicDes = txtDicValueDes.Text;
                        model.IsDelete = !chkValueBox.Checked;
                        BLL.BLLServiceHelper.GetDicService().Save(model);
                    }
                    base.ShowAlertScript("Close_Reload();");
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
        }

        private void CreateDicValue()
        {
            try
            {
                Sys_Dic modelDic = BLL.BLLServiceHelper.GetDicService().GetModelDic(strGuID);
                if (modelDic != null)
                {
                    Sys_Dic model = null;
                    model = new Sys_Dic();
                    model.DicName = txtDicValueName.Value;
                    model.DicDes = txtDicValueDes.Text;
                    model.ParentDicID = modelDic.DicID;
                    model.CreateUser = base.Account;
                    model.IsDelete = !chkValueBox.Checked;
                    BLL.BLLServiceHelper.GetDicService().Save(model);
                    base.ShowAlertScript("Close_Reload();");
                }
                else
                {
                    throw new Exception("获取修改对象实体Sys_Dic为空");
                }
            }
            catch (Exception ex)
            {
                base.ShowAlert("保存数据出错:" + ex.Message);
            }
        }

        private void CreateDic()
        {
            try
            {
                Sys_Dic model = null;
                if (strType == "Edit")
                {
                    model = BLL.BLLServiceHelper.GetDicService().GetModelDic(strGuID);
                }
                else if (strType == "Add")
                {
                    model = new Sys_Dic();
                    model.CreateUser = base.Account;
                }
                model.DicName = txtName.Value;
                model.DicDes = txtDes.Text;
                model.IsDelete = !chkBox.Checked;
                BLL.BLLServiceHelper.GetDicService().Save(model);
                base.ShowAlertScript("Close_Reload();");
            }
            catch (Exception ex)
            {
                base.ShowAlert("保存数据出错:" + ex.Message);
            }
        }
    }
}